package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.StudentObserve;
import cn.wolfcode.crm.mapper.StudentObserveMapper;
import cn.wolfcode.crm.mapper.StudentOfficialMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IStudentObserveService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentObserveServiceImpl implements IStudentObserveService {
    @Autowired
    private StudentObserveMapper studentObserveMapper;

    @Autowired
    private StudentOfficialMapper studentOfficialMapper;

    public void saveOrUpdate(StudentObserve entity) {

    }

    public StudentObserve selectByPrimaryKey(Long id) {
        return studentObserveMapper.selectByPrimaryKey(id);
    }

    public List<StudentObserve> selectAll() {
        return studentObserveMapper.selectAll();
    }


    /**
     * 罗嘉哲
     * 学员升班留级表审核
     * @param id
     * @param newClass
     */
    @Override
    public void changeState(Long id, Long newClass) {

        studentObserveMapper.changeState(id);

        StudentObserve studentObserve = studentObserveMapper.selectByPrimaryKey(id);
        if(studentObserve.getClient()!=null){
            Long clientId = studentObserve.getClient().getId();
            studentOfficialMapper.changeClientClassState(clientId,newClass);
        }
    }

    /**
     * 罗嘉哲
     * 学员升班留级表反审核
     * @param id
     * @param oldClass
     */
    @Override
    public void unchangeState(Long id, Long oldClass) {

        StudentObserve studentObserve = studentObserveMapper.selectByPrimaryKey(id);
        Long clientId = studentObserve.getClient().getId();
        studentObserveMapper.deleteById(id);
        studentOfficialMapper.unchangeClientClassState(clientId,oldClass);
    }

    public PageResult quey4List(QueryObject qo) {
        Integer count = studentObserveMapper.query4Count(qo);
        if (count == 0){
            return PageResult.PAGERESULT_EMPTY;
        }
        List<StudentObserve> rows = (List<StudentObserve>) studentObserveMapper.query4List(qo);
        return new PageResult(count,rows);
    }

    /**
     * 根据条件查询员工列表导出到exl
     * @param qo
     * @return
     */
    public Workbook exportXls(QueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("学员姓名");
        row.createCell(2).setCellValue("总学费");
        row.createCell(3).setCellValue("待缴学费");
        row.createCell(4).setCellValue("已交学费");
        row.createCell(5).setCellValue("联系电话");
        row.createCell(6).setCellValue("以前的班级");
        row.createCell(7).setCellValue("转入的班级");
        row.createCell(8).setCellValue("营销人员");
        row.createCell(9).setCellValue("审核状态");

        List<?> list = studentObserveMapper.query4List(qo);

        for(int i=0;i<list.size();i++){
            try {
                StudentObserve studentObserve = (StudentObserve) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(studentObserve.getId());
                //学员，等完善
                row.createCell(1).setCellValue(studentObserve.getClient()==null?"":studentObserve.getClient().getName());

                row.createCell(2).setCellValue(studentObserve.getTotalAmount().toString());
                row.createCell(3).setCellValue(studentObserve.getWaitngfit().toString());
                row.createCell(4).setCellValue(studentObserve.getPayfit().toString());
                row.createCell(5).setCellValue(studentObserve.getTel());
                row.createCell(6).setCellValue(studentObserve.getOldClass().getName());
                row.createCell(7).setCellValue(studentObserve.getNewClass().getName());
                row.createCell(7).setCellValue(studentObserve.getSaleUser()==null?"":studentObserve.getSaleUser().getUsername());
                row.createCell(6).setCellValue(studentObserve.isState()?"在职":"离职");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return book;
    }

}
